package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostApiPassUpdate;
import com.lc.afbx.util.MEditTextListener;
import com.lc.afbx.util.MyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/5/23.
 */
public class EditPasswordActivity extends BaseActivity implements View.OnClickListener {


    private EditText oldPassword;
    private EditText newPassword;
    private EditText newPassword2;

    private PostApiPassUpdate postApiPassUpdate = new PostApiPassUpdate(new AsyCallBack<PostApiPassUpdate.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiPassUpdate.Info info) throws Exception {

            UtilToast.show(EditPasswordActivity.this, "修改成功");

            BaseApplication.BasePreferences.saveIsSetPay(false);
            BaseApplication.BasePreferences.saveIsLogin(false);
            BaseApplication.BasePreferences.saveCityID("");
            BaseApplication.BasePreferences.saveUserName("");
            BaseApplication.BasePreferences.saveUserId("");

            startActivity(new Intent(EditPasswordActivity.this, LoginActivity.class));

            BaseApplication.INSTANCE.finishActivity(PersonalInfoActivity.class, EditPasswordActivity.class);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);

        oldPassword = ((EditText) findViewById(R.id.txt_oldPassword));
        newPassword = ((EditText) findViewById(R.id.txt_newPassword));
        newPassword2 = ((EditText) findViewById(R.id.txt_newPassword2));

        oldPassword.addTextChangedListener(new MEditTextListener(context, 20, oldPassword));
        newPassword.addTextChangedListener(new MEditTextListener(context, 20, newPassword));
        newPassword2.addTextChangedListener(new MEditTextListener(context, 20, newPassword2));

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.btn_xiugaimima:

                String str1 = newPassword.getText().toString().trim();
                String str2 = newPassword2.getText().toString().trim();

                if (str1.length() < 6) {

                    MyToast.showToast("密码长度不能少于6位", context);
                    return;
                }

                if (str1.equals(str2)) {

                    postApiPassUpdate.user_pass_old = oldPassword.getText().toString().trim();
                    postApiPassUpdate.user_pass_now = str1;
                    postApiPassUpdate.userid = BaseApplication.BasePreferences.readUserId();

                    postApiPassUpdate.execute(this);

                } else {

                    UtilToast.show(EditPasswordActivity.this, "两次输入密码不一致");

                }

                return;

        }


    }


}
